layui.config({
    base: "js/"
}).use(['table', 'layer', 'form', 'jquery'], function () {
    var table = layui.table,
        form = layui.form,
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        $ = layui.jquery;

    var myDate = new Date();
    $(".year").html(myDate.getFullYear());


    $.ajax({
        url: "../../comment/queryCommentAll",
        type: 'get',
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
            console.log(data)
            var str = "";

            /**
             * 循环遍历
             */
            $.each(data.data, function (v, o) {
                str += '<div>';
                str += '<p>' + o.nickName + ':&nbsp;' + o.content + '&nbsp; 发布时间:' + o.createDateTime + '&nbsp; 回复数: ' + o.replyNum + '&nbsp; ' +
                    '点赞: <span class="likeNum">' + o.likeNum + '</span>' + '<a href="javascript:;" class="clickLike" data-id=' + o.userId + '>👍</a>&nbsp;&nbsp;' +
                    '<a href="javascript:;" class="delComment" data-id=' + o.userId + '>删除</a></p>';
                str += '</div>';
            });
            $('.comment').html(str);

            $('.clickLike').click(function () {
                $.ajax({
                    url: "../../comment/clickLine",
                    type: 'put',
                    data: {'userId': $(this).data('id')},
                    success: function (data) {

                        if (data.code == 200) {
                            $.message({
                                message: "点赞成功",
                                type: 'success',
                                showClose: true
                            });
                        }else{
                            $.message({
                                message: data.msg,
                                type: 'warning',
                                showClose: true
                            });
                        }

                    },
                    error: function () {
                        $.message({
                            message: "boom..",
                            type: 'error',
                            showClose: true
                        });

                    }
                });
            })
            
            /**
             * 点击删除评论
             */
            $('.delComment').click(function () {
                $.ajax({
                    url: "../../comment/delComment",
                    type: 'delete',
                    data: {'userId': $(this).data('id')},
                    success: function (data) {
                        if (data.code == 200) {
                            $.message({
                                message: "删除评论成功",
                                type: 'success',
                                showClose: true
                            });
                        }

                    },
                    error: function () {
                        $.message({
                            message: "boom..",
                            type: 'error',
                            showClose: true
                        });

                    }
                });

            })

        },
        error: function () {

        }

    });


    /**
     * 监听提交
     */
    form.on('submit(changeUser)', function (data) {

        if ($("input[name='userName']").val() == '') {
            $.message({
                message: "请输入评论",
                type: 'warning',
                showClose: true
            });
            return false;
        }

        /**
         * 又换了种风格，并且设定最长等待10秒
         */
        var index = layer.load(2, {time: 10 * 5000});

        $.ajax({
            url: "../../comment/saveComment",
            type: 'post',
            dataType: 'json',
            contentType: 'application/json;charset=utf-8',
            data: JSON.stringify(data.field),
            success: function (data) {
                $.message({
                    message: "评论成功",
                    type: 'success',
                    showClose: true
                });
            },
            error: function () {
                $.message({
                    message: "boom..",
                    type: 'error',
                    showClose: true
                });

            }
        });
        layer.close(index);
        return false;
    })


})
;